<template>
  <view class="cart-container" v-if="cartItems.length > 0">
    <!-- 地址信息 -->
    <my-address></my-address>
    <!-- 购物车商品列表的标题区域 -->
    <view class="cart-title">
      <!-- 左侧的图标 -->
      <uni-icons type="shop" size="18"></uni-icons>

      <!-- 描述文本 -->
      <text class="cart-title-text">购物车</text>
    </view>
    <!-- 商品列表区域 -->
    <uni-swipe-action>
      <block v-for="product in cartItems" :key="product.goods_id">
        <uni-swipe-action-item
          :right-options="swipeOptions"
          @click="handleSwipeClick(product)"
        >
          <view class="goods-item">
            <!-- 商品左侧图片区域 -->
            <view class="goods-item-left">
              <radio
                :checked="product.goods_state"
                color="#C00000"
                @click="handleGoodsState(product)"
              ></radio>
              <image
                :src="product.goods_small_logo || defaultImage"
                class="goods-pic"
              ></image>
            </view>

            <!-- 商品右侧信息区域 -->
            <view class="goods-item-right">
              <!-- 商品标题 -->
              <view class="goods-name">{{ product.goods_name }}</view>
              <view class="goods-info-box">
                <!-- 商品价格 -->
                <view class="goods-price">{{
                  product.goods_price | formatPrice
                }}</view>
                <!-- 商品数量 -->
                <!-- 
              $event 
              - 子组件中传递的第一个数据都可以通过$event来接收
              - 大多数子组件都是传递一个事件对象过来

              子组件:
              this.$emit('change', 10)
              父组件:
              <子组件 @change="hanleChange(product, $event)" />
             -->
                <uni-number-box
                  :min="1"
                  :value="product.goods_count"
                  @change="handleChange(product, $event)"
                ></uni-number-box>
              </view>
            </view>
          </view>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>

    <!-- 商品结算区域 -->
    <my-checkout></my-checkout>
  </view>
  <!-- 购物车无商品时 -->
  <view class="empty-cart" v-else>
    <text class="tip-text">空空如也~</text>
  </view>
</template>

<script>
import { mapState } from 'vuex'
import badge from '@/mixins/badge'
import MyAddress from '@/components/my-address.vue'
import MyCheckout from '@/components/my-checkout.vue'
export default {
  mixins: [badge], // 混入
  components: {
    MyAddress,
    MyCheckout,
  },
  data() {
    return {
      // 1. 默认图片
      defaultImage:
        'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
      // 2. 右滑结构配置
      swipeOptions: [
        {
          text: '删除', // 显示的按钮文本
          style: {
            backgroundColor: '#C00000', // 按钮的背景颜色
          },
        },
      ],
    }
  },
  computed: {
    ...mapState('cart', ['cartItems']),
  },
  methods: {
    // 1. 处理单个商品的选中状态
    handleGoodsState(item) {
      // console.log(item)
      // 1.1 提交mutations
      this.$store.commit('cart/updateState', item)
    },
    // 2. 处理单个商品的购买数量
    handleChange(product, num) {
      // console.log(product, num)
      // 2.1 提交mutations
      this.$store.commit('cart/updateCount', {
        product,
        num: +num, // 小技巧: 数量的类型最好在源头控制掉
      })
    },
    // 3. 处理滑动点击操作
    handleSwipeClick(product) {
      // 3.1 提交mutations
      this.$store.commit('cart/removeProduct', product)
    },
  },
}
</script>

<style lang="scss" scoped>
.cart-title {
  height: 80rpx;
  display: flex;
  align-items: center;
  font-size: 28rpx;
  padding-left: 10rpx;
  border-bottom: 1rpx solid #efefef;
  .cart-title-text {
    margin-left: 20rpx;
  }
}

.goods-item {
  display: flex;
  padding: 20rpx 10rpx;
  border-bottom: 1rpx solid #f0f0f0;
  width: 750rpx;
  box-sizing: border-box;

  .goods-item-left {
    margin-right: 10rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .goods-pic {
      width: 200rpx;
      height: 200rpx;
      display: block;
    }
  }

  .goods-item-right {
    display: flex;
    flex: 1;
    flex-direction: column;
    justify-content: space-between;

    .goods-name {
      font-size: 26rpx;
    }

    .goods-info-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }

    .goods-price {
      font-size: 32rpx;
      color: #c00000;
    }
  }
}

.cart-container {
  padding-bottom: 100rpx;
}

.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 300rpx;

  .tip-text {
    font-size: 24rpx;
    color: gray;
    margin-top: 30rpx;
  }
}
</style>
